package com.augurit.wf.client.agency.web;


import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.BaseAction;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.base.extmodel.ExtResult;
import com.augurit.common.base.form.BaseForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.wf.client.agency.service.IWfAgencyService;
import com.augurit.wf.client.agency.web.form.WfAgencyForm;

@SuppressWarnings("serial")
@Results({
	@Result(name = ExtCrudActionSupport.RELOAD, location = "wf-agency.action", type = "redirect"),
	@Result(name = ExtCrudActionSupport.SUCCESS, location = "/agency/WfAgency/WfAgency.jsp"),
	@Result(name = WfAgencyAction.SET_AGENT, location = "/wf/client/agency/part-agency.jsp"),
	@Result(name = ExtCrudActionSupport.INPUT, location = "/agency/WfAgency/WfAgency-input.jsp")
})
public class WfAgencyAction extends ExtCrudActionSupport<WfAgencyForm>{

	private final static String SET_AGENT = "set-agent";
	
	@Autowired
	private IWfAgencyService wfAgencyService;
	
	private WfAgencyForm form;
	private Long id;
	private Long[] ids;
	private Page<WfAgencyForm> page = new Page<WfAgencyForm>(10);

	
	

	/**
	 * 分页查询登录用户的代理列表
	 */
	@Override
	public String list() throws Exception {
		//设置登录用户
		form.setLoginUserCode(this.getLoginUser().getUser().getUserCode());
		
		wfAgencyService.search(page, form);
		ExtBaseAction.renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	/**
	 * 获取所有的用户，本方法应该写在aos中的
	 * @return
	 * @throws Exception
	 */
	public String getAllUsers() throws Exception {
		Page<OmUserForm> users = new Page<OmUserForm>(15);
		ExtUtils.initPageFromExtGridParam(BaseAction.getRequest(), users);
		
		OmUserForm omUserForm = new OmUserForm();
		omUserForm.setUserCode(BaseAction.getRequest().getParameter("userCode"));
		omUserForm.setUserName(BaseAction.getRequest().getParameter("userName"));
		
		wfAgencyService.getAllUsers(users, omUserForm);
		ExtBaseAction.extRenderGridJson(users);
		return null;
	}

	/**
	 * 新增或修改
	 */
	@Override
	public String input() throws Exception {
		
		//此处代码应该写到aos中
		ExtResult result = new ExtResult(true, false);
		List<BaseForm> list = new ArrayList<BaseForm>(1);
		list.add(form);
		result.setResult(list);
		
		ExtBaseAction.renderText(Struts2Utils.getJsonString(result, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}

	/**
	 * 保存新增或者修改的Form对象
	 */
	@Override
	public String save() throws Exception {
		boolean bool = wfAgencyService.saveAgentAndDetails(form);
		if(bool)
			ExtBaseAction.extRenderSuccess();
		else
			ExtBaseAction.extRenderFailture();
		return null;
	}

	/**
	 * 删除一条记录
	 */
	@Override
	public String delete() throws Exception {
		wfAgencyService.deleteAndUpdateJbpm4TaskTable(ids);
		ExtBaseAction.extRenderSuccess();
		return null;
	}
	
	public String setAgent() throws Exception{
		form.setLoginUserCode(this.getLoginUserName());
		form.setLoginUserName(this.getLoginUser().getUser().getUserName());
		return SET_AGENT;
	}
	
	/**
	 * 准备页面模型
	 */
	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			form = wfAgencyService.get(id);
		}
		else
			form = new WfAgencyForm();
	}
	

	public WfAgencyForm getModel() {
		return form;
	}
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Page<WfAgencyForm> getPage() {
		return page;
	}


	public void setPage(Page<WfAgencyForm> page) {
		this.page = page;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}
	
	
}
